from django.db import models
from utils.base import Base

# Create your models here.


# 路径表
class Path(Base):

    title = models.CharField('路径名称',max_length=40,null=True)

    img = models.CharField('logo',max_length=200,null=True)

    desc = models.CharField('简介',max_length=200,null=True)

    # 多对多关联user
    user = models.ManyToManyField('user.User', blank=True)

    def course_total(self):
        count = 0
        for jd in self.stage.all():
            count = count + jd.courses.count()
        return count

    class Meta:

        db_table = 'tb_path'

# 阶段表
class Stage(Base):

    title = models.CharField('阶段名称',max_length=40,null=True)

    number = models.IntegerField('阶段编号',null=True)

    # 路径关联外键
    path = models.ForeignKey(Path,related_name='stage',on_delete=models.SET_NULL,null=True)

    # # 多对多关联课程
    # courses = models.ManyToManyField('course.Course', blank=True)

    class Meta:

        db_table = 'tb_stage'


# 用户及路径关联表
class Accession(Base):

    # 用户关联外键
    user = models.ForeignKey('user.User',on_delete=models.SET_NULL,null=True)

    # 路径关联外键
    path = models.ForeignKey(Path,on_delete=models.SET_NULL,null=True)

    class Meta:

        db_table = 'tb_accession'